package com.emp.exception;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

@Component // 异常处理Bean会被扫描到容器中
//只有被放入spring容器，它才能发挥作用
public class MyHandlerExceptionResolver implements HandlerExceptionResolver {

    private static final Logger logger 
      = LogManager.getLogger(MyHandlerExceptionResolver.class);

    @Override
    public ModelAndView resolveException
    (HttpServletRequest request, 
    HttpServletResponse response, 
    Object handler,
    Exception ex) {//ex系统中发生的异常
        //如果是ajax请求，就返回一个json格式的出错提示信息
        if (request.getHeader("X-Requested-With") != null) {
            try {
               // response.getWriter().println(JsonUtils.toJson(AjaxResult.errorInstance("服务器出错了")));
               response.getWriter().print("FAIL");
            } catch (Exception e) {
                logger.error("服务器失败时发送错误提示信息失败", e);
            }
            //返回一个空的ModelAndView表示已经手动生成响应
            //return null表示使用默认的处理方式，等于没处理
            return new ModelAndView();
        } else {
        	//转发到指定的错误页面
            return new ModelAndView("error");
        }
    }
}